package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ProvinceEntity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.secondary.CommonHolder;

import java.util.List;

public class ChooseAreaAdapter3 extends RecyclerView.Adapter {

    private final Context mContext;
    private int count = 2;
    private int TYPE_ONE = 1;
    private int TYPE_TWO = 2;
    private String areaName = "";
    private List<ProvinceEntity<List<ProvinceEntity>>> provinceEntityList;
    private int select;
    private OnCallBack onCallBack;

    public ChooseAreaAdapter3(Context context, List<ProvinceEntity<List<ProvinceEntity>>> provinceEntityList) {
        this.mContext = context;
        this.provinceEntityList = provinceEntityList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (viewType == TYPE_ONE) {
            viewHolder = CommonHolder.get(mContext, parent, R.layout.holder_choose_area_current);
        } else if (viewType == TYPE_TWO) {
            viewHolder = CommonHolder.get(mContext, parent, R.layout.holder_choose_area_all);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            initOne((CommonHolder) holder);
        } else if (position == 1) {
            initTwo((CommonHolder) holder);
        }
    }

    private LinearLayout rootLayout;
    private TextView nameView;

    //当前省份
    private void initOne(CommonHolder holder) {
        if (rootLayout == null) {
            rootLayout = holder.getItem().findViewById(R.id.root_layout);
            nameView = holder.getItem().findViewById(R.id.name_view);
        }
        nameView.setText(areaName);
    }

    private ProvinceAdapter provinceAdapter;

    //全部省份
    private void initTwo(CommonHolder holder) {
        if (provinceAdapter == null) {
            RecyclerView recyclerView = holder.getItem().findViewById(R.id.recycler_view);
            provinceAdapter = new ProvinceAdapter(mContext, provinceEntityList);
            provinceAdapter.setItemSelect(true);
            final GridLayoutManager manager = new GridLayoutManager(mContext, 3);
            recyclerView.setLayoutManager(manager);
            recyclerView.setAdapter(provinceAdapter);
            provinceAdapter.setOnItemClick(new AdapterOnItemClick<ProvinceEntity<List<ProvinceEntity>>>() {
                @Override
                public void onItemClick(ProvinceEntity<List<ProvinceEntity>> listProvinceEntity, int position) {
                    if (onCallBack != null) {
                        onCallBack.select(listProvinceEntity);
                    }
                }
            });
            provinceAdapter.setDefItem(select);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_ONE;
        } else {
            return TYPE_TWO;
        }
    }

    @Override
    public int getItemCount() {
        return count;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                //如果快速滑动， 不加载图片
                if (newState == 2) {
                    Glide.with(mContext).pauseRequests();
                } else {
                    Glide.with(mContext).resumeRequests();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            }
        });
    }

    public void setCurrentArea(String areaName) {
        if (!areaName.contains("省")) {
            areaName = areaName.substring(0, 2);
        }
        this.areaName = areaName;
        notifyDataSetChanged();
    }

    public void setAllArea(List<ProvinceEntity<List<ProvinceEntity>>> provinceEntityList, int select) {
        provinceAdapter = null;
        this.provinceEntityList = provinceEntityList;
        this.select = select;
        notifyDataSetChanged();
    }

    public interface OnCallBack {
        void select(ProvinceEntity<List<ProvinceEntity>> listProvinceEntity);
    }

    public void setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }
}
